package my.test.csdn.weixin_43767015.basic.ch12.transaction.declarative.annotation.propagation.supports.eachcall.service;

import javax.annotation.Resource;

import org.springframework.aop.framework.AopContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import my.test.csdn.weixin_43767015.basic.ch12.transaction.declarative.annotation.propagation.required.eachcall.service.TxRequiredEachService;

@Component
public class TxSupportsEachService {
    /**
     * jdbc模版，用于操作数据库
     */
    @Resource
    private JdbcTemplate jdbcTemplate;
    
    /** 在开启了事务的方法m1中调用m2：*/
	@Transactional(propagation = Propagation.REQUIRED)
    public void m1() {
        String sql = "insert into tx_study (name,age) values (?,?)";
        jdbcTemplate.update(sql, "TxSupports_m1", 30);
        TxSupportsEachService o =(TxSupportsEachService)AopContext.currentProxy();
        try {
			o.m2();
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //抛出一个RuntimeException
        //throw new RuntimeException();
    }
    
	@Transactional(propagation = Propagation.SUPPORTS)
    public void m2() {
        String sql = "insert into tx_study (name,age) values (?,?)";
        jdbcTemplate.update(sql, "TxSupports_m2", 30);
        //抛出一个RuntimeException
        throw new RuntimeException();
    }
}
